package hk.ust.cse.comp3111.studentSchedule;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.Fragment;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class ViewCourse extends Fragment implements OnClickListener{
	
    
	EditText datePick ;
	Button datePickButton;
	ListView myListView;
	ArrayList<String> listItems;
	TextView output ;
	private long inputDate=0L;
	private final long ONEDAY =86400000L;
	
	
	public ViewCourse(){}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.view_course, container, false);

        
        return rootView;
    }
	
	
	
	
	@Override
	public void onStart() {
		super.onStart();
		
	
		datePick = (EditText) this.getView().findViewById(R.id.datePick);
		datePickButton = (Button) this.getView().findViewById(R.id.datePickButton);
		myListView = (ListView) this.getView().findViewById(R.id.list);
		listItems=new ArrayList<String>();
		output = (TextView) this.getView().findViewById(R.id.output);
		
		datePick.setOnClickListener(this);
		datePickButton.setOnClickListener(this);
	
	}
	


	@Override
	public void onClick(View v) {
		output.setText(datePick.getText());
		
		switch (v.getId()) {
        case R.id.datePickButton:
        	searchEvent();
            break;
        case R.id.datePick:
        	setDate();
        	break;
        	
        }
	}
	
	
	
	private void setDate() {
		Calendar calendar = Calendar.getInstance();
		
		new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener(){
			@Override
			public void onDateSet(DatePicker view, int year, int month, int day){
				datePick.setText(year+"/"+(month+1)+"/"+day);
				Calendar cal = Calendar.getInstance();
				cal.set(year, month, day);
				inputDate = cal.getTimeInMillis();
				output.setText(String.valueOf(inputDate));
			}
		},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
	}
	
	
	
	
	private void searchEvent(){
		
		ArrayAdapter<String> adapter;
		listItems.clear();
        adapter=new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_list_item_1,listItems);
        
        
		/*Cursor cur = null;
		cur = this.getActivity().getContentResolver().query(CalendarContract.Events.CONTENT_URI,
				new String[] { "calendar_id", "title", "description",
                "dtstart", "dtend", Events.RDATE, "eventLocation", Events.RRULE },null,null, null);*/
		
		long now = new Date().getTime();
		Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon();
		ContentUris.appendId(eventsUriBuilder, now - DateUtils.DAY_IN_MILLIS * 10000);
		ContentUris.appendId(eventsUriBuilder, now + DateUtils.DAY_IN_MILLIS * 10000);
		Uri eventsUri = eventsUriBuilder.build();
		Cursor cur = null;       
		cur = this.getActivity().getContentResolver().query(eventsUri, 
				new String[] { "calendar_id", "title", "description",
                "dtstart", "dtend", Events.RDATE, "eventLocation", Events.RRULE }, 
                null, null, CalendarContract.Instances.DTSTART + " ASC");

		
		
		// Uri.parse("content://com.android.calendar/events") ->CalendarContract.Events.CONTENT_URI
		
		if (cur.moveToFirst()) {
            do {
            	
            	/*if(cur.getString(2).toString().contains("by Student Schedule")) {
            		listItems.add(cur.getString(2));
            		listItems.add(cur.getString(3));
            		listItems.add(getTime(cur.getString(3)));
            	}
            	*/
            	
            	listItems.add(cur.getString(1));
            	
            /*if(Long.parseLong(cur.getString(3))>inputDate&&Long.parseLong(cur.getString(3))<inputDate+ONEDAY){
            		listItems.add(cur.getString(1)); // title
            }*/
            	


            	
            } while (cur.moveToNext());
        }
		
		
		
		if(listItems.isEmpty()) listItems.add("Nothing");
		
		
        
        
        myListView.setAdapter(adapter);
        
	}
	
	
	
	
	
	
}
